import redis


def migrate_url_mapping(host='localhost', port=6379, db=5, spider_name='houzz', limit=10):
    """
    将url_mapping数据移动到新的结构下
    """
    # 连接Redis
    r = redis.Redis(host=host, port=port, db=db, decode_responses=True)

    # 查找所有以 'url_mapping:' 开头的键
    old_keys = []
    cursor = 0
    pattern = "url_mapping:*"

    # 使用SCAN避免阻塞
    while True:
        cursor, keys = r.scan(cursor=cursor, match=pattern, count=100)
        old_keys.extend(keys)
        if cursor == 0:
            break

    print(f"找到 {len(old_keys)} 个url_mapping键")

    # 限制处理数量
    processed = 0
    moved_count = 0

    for old_key in old_keys:
        # if processed >= limit:
        #     break

        print(f"\n处理键: {old_key}")

        # 获取集合中的所有成员
        members = r.smembers(old_key)
        print(f"找到 {len(members)} 个成员")

        # 构建新的键名
        # 从 old_key 中提取 detail_url 部分
        if old_key.startswith("url_mapping:"):
            detail_url = old_key.replace("url_mapping:", "", 1)
            new_key = f"{spider_name}:url_mapping:{detail_url}"
        else:
            # 如果键名格式不符合预期，跳过
            print(f"跳过不符合格式的键: {old_key}")
            continue

        # 将成员移动到新键
        for member in members:
            r.sadd(new_key, member)
            moved_count += 1

        # 可选：删除旧键（先注释掉，测试时使用）
        # r.delete(old_key)
        print(f"已将数据移动到: {new_key}")
        print(f"移动了 {len(members)} 个成员")

        processed += 1

    print(f"\n迁移完成！")
    print(f"处理了 {processed} 个键")
    print(f"移动了 {moved_count} 个成员")

    return processed, moved_count


if __name__ == "__main__":
    # 执行迁移，只移动10条
    migrate_url_mapping(limit=10)